﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDemo._03_继承
{
    abstract class SuperClass
    {
        //1.继承
        public int MultiPly(int a, int b)
        {
            return a * b;
        }

        //2.虚方法，子类可以重写也可以不重写
        public virtual int Add(int a, int b)
        {
            return 0;
        }

        //3.方法的隐藏
        public virtual int Subtract(int a, int b)
        {
            return 0;
        }

        //4.抽象类:包含抽象方法，只有被子类继承了才能实例化
        public abstract int Divid(int a, int b);

        //5.密封方法：该方法不能被继承
        public   virtual string SealedMethod()
        {
            return "该方法可以被重写";
        }
    }

    //实现继承(C#只有共有继承)
    class SubClass : SuperClass
    {
        //重写父类方法
        public override int Add(int a, int b)
        {
            return a + b;
        }

        //隐藏父类方法
        public new int Subtract(int a, int b)
        {
            return a - b;
        }

        //实现抽象方法
        public override int Divid(int a, int b)
        {
            if (b == 0)
            {
                throw new Exception("除数为0");
            }
            else
            {
                return a / b;
            }
        }

        //密封类，该方法不能被子类重写了
        public override sealed string SealedMethod()
        {
            return "子类把这个方法变成了密封类";
        }
    }

    class Inheritance
    {
        public static void Test()
        {
            SubClass a = new SubClass();
            Console.WriteLine("\n继承测试");
            Console.WriteLine(a.Add(1, 2));
            Console.WriteLine(a.Subtract(1, 2));
            Console.WriteLine(a.MultiPly(1, 2));
            Console.WriteLine(a.Divid(1, 2));
            Console.WriteLine(a.SealedMethod());
        }
    }
}
